/**
 * 
 */
package com.buss.core.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.buss.base.model.Branch;
import com.buss.core.exception.WebException;
import com.buss.core.model.Role;
import com.buss.core.model.User;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.validator.ResultMessage;

/**
 * 基础Controller
 * @author 高小勇
 * 创建时间:2011-5-2
 * wcms2.0
 */
public abstract class BaseController {

	//修改页面
	protected final static String EDIT_PAGE = "editpage";
	//新增页面
	protected final static String ADD_PAGE = "addpage"; 
	//查看页面
	protected final static String VIEW_PAGE = "viewpage";
	/**
	 * 获取当前用户信息
	 * @param request
	 * @return
	 */
	protected User getUser(HttpServletRequest request){
		return (User)request.getSession().getAttribute(SysStaticParam.SESSION_USER);
	}
	
	/**
	 * 获取当前登录用户ID
	 * @param request
	 * @return
	 */
	protected String userId(HttpServletRequest request){
		User user = this.getUser(request);
		if(user == null){
			return "";
		}
		return user.getUserId();
	}
	
	/**
	 * 获取当前登录用户名称
	 * @param request
	 * @return
	 */
	protected String userName(HttpServletRequest request){
		User user = this.getUser(request);
		if(user == null){
			return null;
		}
		return user.getUserName();
	}
	
	/**
	 * 获取用户所在单位编码
	 * @param request
	 * @return
	 */
	protected String getCompanyId(HttpServletRequest request){
		User user = getUser(request);
		return user.getCompanyId();
	}
	
	/**
	 * 获取当前店铺信息
	 * @param request
	 * @return
	 */
	protected Branch getBranch(HttpServletRequest request){
		Branch branch = (Branch)request.getSession().getAttribute(SysStaticParam.SESSION_BRANCH);
		return branch;
	}
	
	/**
	 * 获取当前店铺的ID
	 * @param request
	 * @return
	 */
	protected Long getBranchId(HttpServletRequest request){
		Branch branch = getBranch(request);
		if(branch != null){
			return branch.getId();
		}
		return -1L;
	}
	
	/**
	 * 获取当前店铺的Name
	 * @param request
	 * @return
	 */
	protected String getBranchName(HttpServletRequest request){
		Branch branch = getBranch(request);
		if(branch != null){
			return branch.getName();
		}
		return "未知店铺";
	}
	
	/**
	 * 获取当前登录用户所属角色信息列表
	 * @param request
	 * @return
	 */
	protected List<Role> getRoles(HttpServletRequest request){
		User user = this.getUser(request);
		if(user == null){
			return null;
		}
		return user.getRoles();
	}
	
	/**
	 * 设置返回消息信息
	 * @param request
	 * @param code
	 * @param message
	 */
	protected void setResultMessage(HttpServletRequest request,String code,String message){
		ResultMessage resultMessage = new ResultMessage(code, message);
		request.setAttribute(SysStaticParam.RESULT_MESSAGE, resultMessage);
	}
	
	/**
	 * 获取用户对应角色编码
	 * @param request
	 * @return
	 */
	protected Long[] getUserRoleIds(HttpServletRequest request){
		User user = this.getUser(request);
		Long[] roleIds = new Long[user.getRoles().size()];
		for(int i = 0;i < user.getRoles().size();i++){
			roleIds[i] = user.getRoles().get(i).getId();
		}
		return roleIds;
	}
	
	/**
	 * 返回列表页面
	 * @return
	 */
	public abstract ModelAndView listPage(@RequestParam(required=false) String menuid);
	
	/**
	 * 新增、编辑页面
	 * @param type
	 * @param id
	 * @return
	 */
	public ModelAndView editPage(@PathVariable String type,@RequestParam(required=false) Long id){
		return null;
	};
	
	/**
	 * 根据多个ID删除数据
	 * @param ids
	 * @return
	 */
	public abstract Map<String, Object> delete(@RequestParam Long[] ids)  throws WebException;
}
